package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {

    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private DailyContentMapper dailyContentMapper;

    @Override
    public PageInfo query(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Daily> list = dailyMapper.selectForList(qo);
        for (Daily daily : list) {
            int count = dailyMapper.selectCountByDailyId(daily.getId());
            daily.setCollection(count);
        }
        return new PageInfo(list);
    }

    @Override
    public DailyContent getDailyContentByDailyId(Long dailyId) {
        return dailyContentMapper.selectByPrimaryKey(dailyId);
    }

    @Override
    public void saveOrUpdate(Daily daily) {
        daily.setReleaseTime(new Date());
        //获取日报的内容
        DailyContent dailyContent = daily.getDailyContent();

        if(daily.getId()==null){
            dailyMapper.insert(daily);
            //新增日报内容
            dailyContent.setId(daily.getId());
            dailyContentMapper.insert(dailyContent);
        }else{
            dailyMapper.updateByPrimaryKey(daily);
            //同步更新内容
            dailyContent.setId(daily.getId());
            dailyContentMapper.updateByPrimaryKey(dailyContent);
        }
    }

    @Override
    public JSONResult saveOrDeleteCollection(Long userId, Long dailyId) {
        JSONResult result =new JSONResult();
        int count = dailyMapper.selectCountByDailyIdAndUserId(userId, dailyId);
        if (count==0){
            Date date = new Date();
            dailyMapper.saveCollection(userId, dailyId,date);
            result.mark("收藏成功");
        }else{
            dailyMapper.deleteCollection(userId, dailyId);
            result.mark("取消收藏");
        }
        return result;
    }

    @Override
    public List<Long> selectCollectionByUserId(Long userId) {
        List<Daily> dailys = dailyMapper.selectCollectionByUserId(userId);
        List<Long> ids=new ArrayList<>();
        for (Daily daily : dailys) {
            ids.add(daily.getId());
        }
        return ids;
    }

    @Override
    public Daily get(Long id) {
        Daily daily = dailyMapper.selectByPrimaryKey(id);
        DailyContent dailyContent = dailyContentMapper.selectByPrimaryKey(id);
        daily.setDailyContent(dailyContent);
        return daily;
    }

    @Override
    public PageInfo queryCollection(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List list = dailyMapper.selectCollectionDailyByUserId(qo);
        return new PageInfo(list);
    }
}
